package com.moxiao.yfjh.module.blind.service.sessionRecord;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.module.blind.controller.admin.sessionRecord.vo.SessionRecordCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.sessionRecord.vo.SessionRecordExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.sessionRecord.vo.SessionRecordPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.sessionRecord.vo.SessionRecordUpdateReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.sessionRecord.SessionRecordDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.sessionRecord.vo.*;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.blind.convert.sessionRecord.SessionRecordConvert;
import com.moxiao.yfjh.module.blind.dal.mysql.sessionRecord.SessionRecordMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲会话记录 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class SessionRecordServiceImpl implements SessionRecordService {

    @Resource
    private SessionRecordMapper sessionRecordMapper;

    @Override
    public Long createSessionRecord(SessionRecordCreateReqVO createReqVO) {
        // 插入
        SessionRecordDO sessionRecord = SessionRecordConvert.INSTANCE.convert(createReqVO);
        sessionRecordMapper.insert(sessionRecord);
        // 返回
        return sessionRecord.getId();
    }

    @Override
    public void updateSessionRecord(SessionRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateSessionRecordExists(updateReqVO.getId());
        // 更新
        SessionRecordDO updateObj = SessionRecordConvert.INSTANCE.convert(updateReqVO);
        sessionRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteSessionRecord(Long id) {
        // 校验存在
        validateSessionRecordExists(id);
        // 删除
        sessionRecordMapper.deleteById(id);
    }

    private void validateSessionRecordExists(Long id) {
        if (sessionRecordMapper.selectById(id) == null) {
            throw exception(SESSION_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public SessionRecordDO getSessionRecord(Long id) {
        return sessionRecordMapper.selectById(id);
    }

    @Override
    public List<SessionRecordDO> getSessionRecordList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return sessionRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SessionRecordDO> getSessionRecordPage(SessionRecordPageReqVO pageReqVO) {
        return sessionRecordMapper.selectPage(pageReqVO);
    }
    @Override
    public PageResult<SessionRecordDO> getSessionRecordPageByUserId(SessionRecordPageReqVO pageReqVO) {
        return sessionRecordMapper.selectPageByUserId(pageReqVO);
    }

    @Override
    public List<SessionRecordDO> getSessionRecordList(SessionRecordExportReqVO exportReqVO) {
        return sessionRecordMapper.selectList(exportReqVO);
    }

    @Override
    public SessionRecordDO getSessionRecordTodayLast(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<SessionRecordDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(SessionRecordDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(SessionRecordDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return sessionRecordMapper.selectOne(lqw);
    }

    @Override
    public Integer getSessionRecordNo(String sessionNo) {
        return sessionRecordMapper.getSessionRecordNo(sessionNo);
    }

    @Override
    public List<SessionRecordDO> getRecordList(SessionRecordExportReqVO exportReqVO) {
        return sessionRecordMapper.selectRecordList(exportReqVO.getFounder(),exportReqVO.getParticipants());
    }

}
